﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace EmptoolWebApp.Controllers
{
    [Authorize]
    public class CompanyController : BaseController
    {
        // GET: Company/About
        public ActionResult About()
        {
            return View();
        }

        // GET: Company/List
        public ActionResult List()
        {
            EmptoolWebApp.Models.CompanyListViewModel vm = new EmptoolWebApp.Models.CompanyListViewModel();

            Microsoft.WindowsAzure.Storage.Table.CloudTable tbl =
                Helpers.AzureStorageHelper.GetStorageTable(Entities.CompanyEntity.TABLE_NAME);

            Microsoft.WindowsAzure.Storage.Table.TableQuery<EmptoolWebApp.Entities.CompanyEntity> query =
                new Microsoft.WindowsAzure.Storage.Table.TableQuery<Entities.CompanyEntity>();
            List<EmptoolWebApp.Entities.CompanyEntity> list = tbl.ExecuteQuery<EmptoolWebApp.Entities.CompanyEntity>(query).ToList();

            vm.CompanyEntityList = list;

            return View(vm);
        }

        // GET: Company
        public ActionResult Index()
        {
            return RedirectToAction("List");
        }

        // GET: Company/Details/5
        public ActionResult Details(string id)
        {
            Microsoft.WindowsAzure.Storage.Table.CloudTable tbl =
    Helpers.AzureStorageHelper.GetStorageTable(Entities.CompanyEntity.TABLE_NAME);
            Microsoft.WindowsAzure.Storage.Table.TableOperation retrieveOperation =
                Microsoft.WindowsAzure.Storage.Table.TableOperation.Retrieve<EmptoolWebApp.Entities.CompanyEntity>(id, string.Empty);
            Microsoft.WindowsAzure.Storage.Table.TableResult retrievedResult = tbl.Execute(retrieveOperation);
            EmptoolWebApp.Entities.CompanyEntity companyEntity = retrievedResult.Result as EmptoolWebApp.Entities.CompanyEntity;

            EmptoolWebApp.Models.CompanyViewModel vm = new EmptoolWebApp.Models.CompanyViewModel(companyEntity);

            return View(vm);
        }

        // GET: Company/Create
        public ActionResult Create()
        {
            EmptoolWebApp.Models.CompanyViewModel vm = new Models.CompanyViewModel();

            return View(vm);
        }

        // POST: Company/Create
        [HttpPost]
        public ActionResult Create(EmptoolWebApp.Models.CompanyViewModel model)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    // Copy model data to table storage entity
                    EmptoolWebApp.Entities.CompanyEntity company = new Entities.CompanyEntity(model.CompanyName);
                    company.Owner = User.Identity.Name;
                    company.Creator = User.Identity.Name;

                    EmptoolWebApp.Entities.CompanyAssociateEntity companyAssociate = new Entities.CompanyAssociateEntity(
                        model.CompanyName, User.Identity.Name, Entities.CompanyAssociationType.EMPLOYEE);
                    companyAssociate.Owner = User.Identity.Name;
                    companyAssociate.Creator = User.Identity.Name;

                    try
                    {
#if DEBUG
                        // Step-by-step syntax
                        Microsoft.WindowsAzure.Storage.Table.TableOperation insertOperation =
                            Microsoft.WindowsAzure.Storage.Table.TableOperation.Insert(company);
                        Microsoft.WindowsAzure.Storage.Table.CloudTable tbl =
                            Helpers.AzureStorageHelper.GetStorageTable(Entities.CompanyEntity.TABLE_NAME);
                        tbl.Execute(insertOperation);

                        insertOperation = Microsoft.WindowsAzure.Storage.Table.TableOperation.Insert(companyAssociate);
                        tbl = Helpers.AzureStorageHelper.GetStorageTable(Entities.CompanyAssociateEntity.TABLE_NAME);
                        tbl.Execute(insertOperation);
#else
                        // Fluent syntax
                        Helpers.AzureStorageHelper.GetStorageTable(Entities.CompanyEntity.TABLE_NAME).Execute(
                            Microsoft.WindowsAzure.Storage.Table.TableOperation.Insert(company));
#endif
                    }
                    catch (Exception ex)
                    {
                        TempData["ERROR_MESSAGE"] = ex.Message;
                        throw;
                    }

                }

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        // GET: Company/Edit/5
        public ActionResult Edit(string id)
        {
            Microsoft.WindowsAzure.Storage.Table.CloudTable tbl =
                Helpers.AzureStorageHelper.GetStorageTable(Entities.CompanyEntity.TABLE_NAME);
            Microsoft.WindowsAzure.Storage.Table.TableOperation retrieveOperation =
                Microsoft.WindowsAzure.Storage.Table.TableOperation.Retrieve<EmptoolWebApp.Entities.CompanyEntity>(id, string.Empty);
            Microsoft.WindowsAzure.Storage.Table.TableResult retrievedResult = tbl.Execute(retrieveOperation);
            EmptoolWebApp.Entities.CompanyEntity companyEntity = retrievedResult.Result as EmptoolWebApp.Entities.CompanyEntity;

            EmptoolWebApp.Models.CompanyViewModel vm = new EmptoolWebApp.Models.CompanyViewModel(companyEntity);

            return View(vm);
        }

        // POST: Company/Edit/5
        [HttpPost]
        public ActionResult Edit(string id, Models.CompanyViewModel vm)
        {
            try
            {
                // TODO: Add update logic here
                Microsoft.WindowsAzure.Storage.Table.CloudTable tbl =
                Helpers.AzureStorageHelper.GetStorageTable(Entities.CompanyEntity.TABLE_NAME);
                Microsoft.WindowsAzure.Storage.Table.TableOperation retrieveOperation =
                    Microsoft.WindowsAzure.Storage.Table.TableOperation.Retrieve<EmptoolWebApp.Entities.CompanyEntity>(id, string.Empty);
                Microsoft.WindowsAzure.Storage.Table.TableResult retrievedResult = tbl.Execute(retrieveOperation);
                EmptoolWebApp.Entities.CompanyEntity companyEntity = retrievedResult.Result as EmptoolWebApp.Entities.CompanyEntity;
                if (companyEntity != null)
                {
                    companyEntity.CompanyName = vm.CompanyName;

                    Microsoft.WindowsAzure.Storage.Table.TableOperation updateOperation =
                        Microsoft.WindowsAzure.Storage.Table.TableOperation.Replace(companyEntity);
                    tbl.Execute(updateOperation);
                }

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        // GET: Company/Delete/5
        public ActionResult Delete(string id)
        {
            Microsoft.WindowsAzure.Storage.Table.CloudTable tbl =
                Helpers.AzureStorageHelper.GetStorageTable(Entities.CompanyEntity.TABLE_NAME);
            Microsoft.WindowsAzure.Storage.Table.TableOperation retrieveOperation =
                Microsoft.WindowsAzure.Storage.Table.TableOperation.Retrieve<EmptoolWebApp.Entities.CompanyEntity>(id, string.Empty);
            Microsoft.WindowsAzure.Storage.Table.TableResult retrievedResult = tbl.Execute(retrieveOperation);
            EmptoolWebApp.Entities.CompanyEntity companyEntity = retrievedResult.Result as EmptoolWebApp.Entities.CompanyEntity;

            EmptoolWebApp.Models.CompanyViewModel vm = new EmptoolWebApp.Models.CompanyViewModel(companyEntity);

            return View(vm);
        }

        // POST: Company/Delete/5
        [HttpPost]
        public ActionResult Delete(string id, Models.CompanyViewModel vm)
        {
            try
            {
                // TODO: Add delete logic here
                Microsoft.WindowsAzure.Storage.Table.CloudTable tbl =
                    Helpers.AzureStorageHelper.GetStorageTable(Entities.CompanyEntity.TABLE_NAME);
                Microsoft.WindowsAzure.Storage.Table.TableOperation retrieveOperation =
                    Microsoft.WindowsAzure.Storage.Table.TableOperation.Retrieve<EmptoolWebApp.Entities.CompanyEntity>(id, string.Empty);
                Microsoft.WindowsAzure.Storage.Table.TableResult retrievedResult = tbl.Execute(retrieveOperation);
                EmptoolWebApp.Entities.CompanyEntity companyEntity = retrievedResult.Result as EmptoolWebApp.Entities.CompanyEntity;
                if (companyEntity != null)
                {
                    Microsoft.WindowsAzure.Storage.Table.TableOperation deleteOperation =
                        Microsoft.WindowsAzure.Storage.Table.TableOperation.Delete(companyEntity);
                    tbl.Execute(deleteOperation);
                }

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
